﻿
Imports System.Data.SqlClient

Public Class frmSearchProspect
    Inherits System.Windows.Forms.Form

    Public ProspectType As String

    Sub Fill()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select SNo,Name,Type From Prospects " & _
                                      "Where Name like N'%" & Me.txtCustName.Text & "%' and Type Like N'%" & ProspectType & "%' Order by Name", cnn)
            Dim Reader As SqlDataReader
            Me.ListView1.Items.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While (Reader.Read)
                With ListView1.Items.Add(Reader.Item(0))
                    .SubItems.Add(Reader.Item(1))
                    .SubItems.Add(Reader.Item(2))
                End With
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        If Len(Me.txtCustName.Text.Trim) = 0 Then
            Me.ErrorProv.SetError(Me.txtCustName, "Please fill in")
            Me.txtCustName.Focus()
        End If

        Fill()
    End Sub

    Private Sub frmSearchCust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SelProspectID = 0
        SelProspectName = ""
        Me.txtCustName.Focus()
    End Sub

    Private Sub ListView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
        If Me.ListView1.SelectedIndices.Count <> 0 Then
            SelProspectID = Me.ListView1.SelectedItems.Item(0).Text
            SelProspectName = Me.ListView1.SelectedItems.Item(0).SubItems(1).Text
            Me.Close()
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub txtCustName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCustName.TextChanged
        Me.ErrorProv.Clear()
    End Sub
End Class
